package Sunday;

//这个类就是存放算法的类
public class SundayText {

    public int OldSunday(String haystack, String needle) {
        int hayLen = haystack.length();//主串长度
        int nLen = needle.length();//子串长度
        int i=0;//记录主串比较初始位置
        int j=0;//查找失败的时查看后一个字符在字串中是否存在的变量
        if(hayLen>nLen)
        {
            while(i<hayLen-nLen) {
                if (needle.charAt(0) == haystack.charAt(i))
                {
                    for (int n = 1; n < nLen; n++) {
                        if (needle.charAt(n) != haystack.charAt(n+i)) {
                            for (j = 0; j < nLen; j++) {
                                if (needle.charAt(j) == haystack.charAt(i+nLen)) {
                                    i=i+nLen-j;
                                    break;
                                }
                            }
                            i= i+nLen+1;
                            break;
                        }
                        else{
                            if(n==nLen-1){
                                return i;
                            }
                            continue;
                        }
                    }
                } else {
                    boolean bool=false;
                    for (j =0; j<nLen; j++) {
                        if (needle.charAt(j) == haystack.charAt(i+nLen)) {
                            i=i+nLen-j;
                            bool=true;
                            break;
                        }
                    }
                    if(bool==false){
                        i= i+nLen+1;
                    }
                }
            }
            return i;
        }
        else{
            System.out.println("后者比前者长，不能进行查找操作");
            return -1;
        }
    }


    public int NewSunday(String haystack, String needle) {
        int hayLen = haystack.length();//主串长度
        int nLen = needle.length();//子串长度
        int i=nLen-1;
        int l=0;
        int j=0;
        if(hayLen>nLen)
        {
            while(i<hayLen) {
                l=i;
                for (j=nLen-1;j>=0;j--)
                {
                    if (needle.charAt(j)==haystack.charAt(i)) {
                        i += nLen - 1 - j;
                        if (i < hayLen){
                            for (int n = nLen - 1; n >= 0; n--) {
                                if (needle.charAt(n) != haystack.charAt(i--)) {
                                    break;
                                } else if (n == 0) {
                                    return i + 1;
                                }
                            }
                            i = l;
                            break;
                        }
                        else{
                            return -1;
                        }
                    }
                }
                i+=nLen;
                continue;
            }
            return -1;
        }
        else{
            System.out.println("后者比前者长，不能进行查找操作");
            return -1;
        }
    }

}
